﻿#define DebugWrite

using log4net;
using log4net.Appender;
using log4net.Repository.Hierarchy;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CP_PcsRate_Api.AppCode
{
    public class LogHelper
    {
        public static string SystemName = "";

        public static void Debug(string message, string remark = "")
        {
            log4net.LogicalThreadContext.Properties["SystemName"] = SystemName;
            log4net.LogicalThreadContext.Properties["Remark"] = remark;
            ILog log = LogManager.GetLogger("Debug");
            if (log.IsDebugEnabled)
            {
#if DebugWrite
                Console.WriteLine(message);
                if (!string.IsNullOrEmpty(remark))
                {
                    Console.WriteLine(remark);
                }
#endif
                log.Debug(message);
            }
        }

        // 错误日志带异常
        public static void Debug(string message, Exception ex, string remark = "")
        {
            log4net.LogicalThreadContext.Properties["SystemName"] = SystemName;
            log4net.LogicalThreadContext.Properties["Remark"] = remark;
            ILog log = LogManager.GetLogger("ERROR");
            if (log.IsDebugEnabled)
            {
#if DebugWrite
                Console.WriteLine(message);
                if (!string.IsNullOrEmpty(remark))
                {
                    Console.WriteLine(remark);
                }
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
#endif
                log.Error(message, ex);
            }
        }

        // 错误日志带异常
        public static void Debug(Exception ex, string remark = "")
        {
            log4net.LogicalThreadContext.Properties["SystemName"] = SystemName;
            log4net.LogicalThreadContext.Properties["Remark"] = remark;
            ILog log = LogManager.GetLogger("ERROR");
            if (log.IsDebugEnabled)
            {
#if DebugWrite
                Console.WriteLine(ex.Message);
                if (!string.IsNullOrEmpty(remark))
                {
                    Console.WriteLine(remark);
                }
                Console.WriteLine(ex.StackTrace);
#endif
                log.Error(ex.Message, ex);
            }
        }

        public static void SetUpDbConnection(string connectionString, string logConfig)
        {
            try
            {
                //update connection string for log4net dynamically
                var hier = LogManager.GetRepository() as Hierarchy;
                log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(logConfig));
                if (hier != null)
                {
                    var adoNetAppenders = hier.GetAppenders().OfType<AdoNetAppender>();
                    foreach (var adoNetAppender in adoNetAppenders)
                    {
                        adoNetAppender.ConnectionString = connectionString;
                        adoNetAppender.ActivateOptions();
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }


    }

    public class LogHelperWrite
    {
        public static void Debug(string message, string remark = "")
        {
            log4net.LogicalThreadContext.Properties["SystemName"] = LogHelper.SystemName;
            log4net.LogicalThreadContext.Properties["Remark"] = remark;
            ILog log = LogManager.GetLogger("Debug");
            if (log.IsDebugEnabled)
            {
                Console.WriteLine(message);
                if (!string.IsNullOrEmpty(remark))
                {
                    Console.WriteLine(remark);
                }
                log.Debug(message);
            }
        }

        // 错误日志带异常
        public static void Debug(string message, Exception ex, string remark = "")
        {
            log4net.LogicalThreadContext.Properties["SystemName"] = LogHelper.SystemName;
            log4net.LogicalThreadContext.Properties["Remark"] = remark;
            ILog log = LogManager.GetLogger("ERROR");
            if (log.IsDebugEnabled)
            {
                Console.WriteLine(message);
                if (!string.IsNullOrEmpty(remark))
                {
                    Console.WriteLine(remark);
                }
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                log.Error(message, ex);
            }
        }

        // 错误日志带异常
        public static void Debug(Exception ex, string remark = "")
        {
            log4net.LogicalThreadContext.Properties["SystemName"] = LogHelper.SystemName;
            log4net.LogicalThreadContext.Properties["Remark"] = remark;
            ILog log = LogManager.GetLogger("ERROR");
            if (log.IsDebugEnabled)
            {
                Console.WriteLine(ex.Message);
                if (!string.IsNullOrEmpty(remark))
                {
                    Console.WriteLine(remark);
                }
                Console.WriteLine(ex.StackTrace);
                log.Error(ex.Message, ex);
            }
        }

    }
}
